package com.qingxin.inspection.action;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qingxin.cms.entity.InspectionItem;
import com.qingxin.cms.entity.InspectionItemAttachment;
import com.qingxin.inspection.service.InspectionItemService;
import com.qingxin.park.entity.FarmPark;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.entity.MyUploadFile;
import com.qingxin.sys.entity.MyUploadFileParam;
import com.qingxin.sys.entity.MyUploadFiles;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;

/**
 * 巡查管理
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/inspection/item")
public class InspectionItemAction extends BaseAction {
	
	@Autowired
	private InspectionItemService inspectionItemService;
	@Autowired
	private InspectionItemAttachmentService attachmentService;
	
	
	
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "trace/inspection/item/item_list";
	}
	/**
	 * 获取巡查项目 分页数据
	 * @param pageParam
	 * @param search
	 * @param leftdata
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(PageParam pageParam,String search,String leftdata,HttpServletRequest request){
		return inspectionItemService.list(pageParam, search, leftdata, request);
	}
	

	/**
	 * 添加巡查项目
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, String leftdata, HttpServletRequest request){
		model.put("taskId", leftdata);
		return "/trace/inspection/item/item_add";
	}
	
	/**
	 * 保存项目信息
	 * @param item
	 * @param result
	 * @param taskId
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method=RequestMethod.POST)
	@ResponseBody
	public Object save(@Valid InspectionItem item ,BindingResult result, String taskId,String parkid ,HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		MyUploadFileParam uploadFileParam = new MyUploadFileParam("attachment", request);

		SysError info = inspectionItemService.add(item,taskId,parkid,uploadFileParam,getLoginUser(request), request);	
		return info;
	}
	
	/**
	 * 修改巡查项目
	 * @param model
	 * @param leftdata
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model,String itemId, HttpServletRequest request){
		InspectionItem item = inspectionItemService.get(itemId);
		model.put("item", item);
		
		List<InspectionItemAttachment> list = attachmentService.list(itemId);
		MyUploadFiles attachments = new MyUploadFiles();
		for(InspectionItemAttachment att : list){
			attachments.addFile(new MyUploadFile(att.getId(), att.getFilePath(), att.getFilePath(), att.getFileName(), null));
		}
		model.put("attachments", attachments);
		
		return "/trace/inspection/item/item_edit";
	}
	
	/**
	 * 保存修改
	 * @param item
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid InspectionItem item,String parkid, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		MyUploadFileParam uploadFileParam = new MyUploadFileParam("attachment", request);
		return inspectionItemService.update(item,parkid,uploadFileParam, getLoginUser(request), request);
	}
	
	
	/**
	 * 删除文章
	 * @param itemId
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String[] itemId, HttpServletRequest request){
		return inspectionItemService.delete(itemId, request);
	}
	
	
	/**
	 * 获取园区
	 * @param parkid
	 * @return
	 */
	@RequestMapping(value = "/getParkData.json", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public List<FarmPark> getParkData(HttpServletRequest request){
		return inspectionItemService.getParkData(getLoginUser(request));
	}
	
	/**
	 * 判断项目名称是否存在
	 * @param itemName
	 * @param itemId
	 * @param taskId
	 * @return
	 */
	@RequestMapping(value="/isNameDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isNameDuplicate(String itemName, String itemId, String taskId){
		boolean existed = inspectionItemService.isNameDuplicate(itemName, itemId, taskId);
		return existed ? "1":"0";
	}
	
}
